import {defineConfig, loadEnv} from 'vite';
import path from 'path'
import createVitePlugins from './vite/plugins'

export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_ENV, VITE_PORT, VITE_BASE_API } = env

  return defineConfig({
    base: VITE_ENV === 'production' ? '/' : '/',
    plugins: createVitePlugins(env, command === 'build'),
    server: {
      host: true,
      port: VITE_PORT,
      open: false,
      proxy: {
        "/api": {
          target: `http://127.0.0.1:8088`,
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/api/, '')
        }
      }
    },
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, './src')
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    //打包前先清空原有打包文件
    build: {
      emptyOutDir: true,
    }
  });
};
